var app = getApp();
// var Notify = require('../../../../dist/notify/index')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coupon: [],
    isShowNoTest: false,
    isLoadMore: true, // 判断是否能够加载更多
    last_page: 0, // 总页数
    page: 1,
    coupon_length: '', // 优惠券总数
    show: false,
    is_publish: false,//优惠券按钮是否开启
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData() {
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.center.coupon.index');
    urlStr += '&page=1';
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          for (var i = 0;i < response.data.data.length;i++) {
            response.data.data[i].edit_active = false;
          }
          if (!this.data.last_page) {
            this.setData({
              last_page: 0
            });
          }
          this.setData({
            is_publish: response.data.publish
          });
          if (response.data.data.length > 0) {
            this.setData({
              isShowNoTest: false,
              isLoadMore: true,
              last_page: response.data.last_page,
              coupon: response.data.data,
              coupon_length: response.data.total,
              is_publish: response.data.publish
            });
          }
          if (response.data.data.length <= 0) {
            this.setData({
              isShowNoTest: true,
              coupon_length: 0
            });
          }
        }else if (response.result === 0) {
          if (response.data.url) {
            wx.navigateTo({
              url: response.data.url
            });
          } else {
            wx.showToast({
              title: response.msg,
              icon: 'none'
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.last_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.center.coupon.index');
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            for (var i = 0;i < response.data.data.length;i++) {
              response.data.data[i].edit_active = false;
            }
            let coupon = this.data.coupon.concat(response.data.data);
            this.setData({ coupon: coupon});
          } else {
            this.setData({
              page: this.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  moreEvent(e) {
    let index = e.target.dataset.index || e.currentTarget.dataset.index;
    if (this.data.coupon[index].edit_active == false) {
      this.emptyEditActive();
      let coupon = this.data.coupon;
      coupon[index].edit_active = true;
      this.setData({
        coupon});
    }else {
      this.emptyEditActive();
    }
  },
  emptyEditActive() {
    for (var i = 0; i < this.data.coupon.length; i++) {
      let coups = this.data.coupon;
      coups[i].edit_active = false;
      this.setData({
        coupon: coups
      });
    }
  },
  Toedit(e) {
    let id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageB/member/commodity_coupon/commodityCoupon_edit/commodityCoupon_edit' + '?id=' + id
    });
  },
  // 删除按钮被点击的时候
  commodityDelete(e) {
    var index = e.target.dataset.index || e.currentTarget.dataset.index;
    this.setData({
      show: true,
      index: index
    });
  },
  // 跳转到发布优惠券
  goToCouponEdit(e) {
    let id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageB/member/commodity_coupon/commodityCoupon_edit/commodityCoupon_edit' + '?id=' + id
    });
  },
  onCloseShow() {
    this.setData({
      show: false
    });
  },
  // 取消
  confimCancel() {
    this.setData({
      show: false
    });
    wx.showToast({
      title: '已取消删除',
      icon: 'none'
    });
  },
  // 确认删除
  confimCanfim() {
    let i = this.data.index;
    let id = this.data.coupon[i].id;
    this.setData({
      show: false
    });
    let json = {id: id};
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.center.coupon.destroy');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.getData();
          wx.showToast({
            title: '优惠券删除成功',
            icon: 'none'
          });
        } else if (response.result === 0) {
          if (response.data.url) {

            // window.location.href = response.data.url
          } else {
            wx.showToast({
              title: response.msg,
              icon: 'none'
            });
          }
        }else {
          // that.$message.error(response.msg)
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  provide(e) {
    let id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageB/member/commodity_coupon/CommodityCouponGive/CommodityCouponGive' + '?id=' + id
    });
  }
});
